/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Labsystem.Medicos;

import Entidades.Especialidad;
import Entidades.Medico;
import Labsystem.DaoImpl;
import java.awt.Toolkit;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;


public class ModificarMedico extends javax.swing.JDialog {

  
    private static Medico medico = null;
    private DaoImpl dao = new DaoImpl();
    
    public ModificarMedico(java.awt.Dialog parent, boolean modal, Medico medico) {
        super(parent, modal);
        initComponents();
        
        //Establece los campos de modificacion con los valores del medico que se selecciono para modificar
        this.medico = medico;
        textoNumRegistro.setText(Integer.toString(medico.getNumRegistro()));
        textoNumCedula.setText(Integer.toString(medico.getCedula()));
        textoNombre.setText(medico.getNombre());
        textoApellido.setText(medico.getApellido());
        textoDireccion.setText(medico.getDireccion());
        textoCorreo.setText(medico.getCorreo());
        textoTelefono.setText(medico.getTelefono());
        
        //Establece el sexo del medico en la tabla de modificaciones
        if(medico.getSexo().toLowerCase().equals("masculino")) sexo.setSelectedIndex(0);
        else sexo.setSelectedIndex(1);
        
        //Carga las Especialidaes en la lista de especialidades
        actualizarEspercialidades();
        
        //Establece la especialidad del medico
        int indice = 0;
        List lista = dao.ejecutarQuery("Select descripcion from Especialidad");
        
        for(Object obj : lista){
            if(medico.getEspecialidad().getDescripcion().equals(obj.toString())) {
                 especialidad.setSelectedIndex(indice);
                 break;
            }
             indice++;
        }
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        textoNumRegistro = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        textoNumCedula = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        textoNombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        textoApellido = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        sexo = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        textoDireccion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        textoTelefono = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        especialidad = new javax.swing.JComboBox();
        agregarEspecialidad = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        textoCorreo = new javax.swing.JTextField();
        Confirmar = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "ModificarMedico", 0, 0, new java.awt.Font("Ubuntu Light", 0, 14), new java.awt.Color(51, 51, 51))); // NOI18N
        jPanel1.setForeground(new java.awt.Color(0, 153, 255));
        jPanel1.setFont(new java.awt.Font("Ubuntu Light", 0, 14)); // NOI18N

        jLabel1.setText("Numero de Registro :");

        textoNumRegistro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textoNumRegistroKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textoNumRegistroKeyTyped(evt);
            }
        });

        jLabel2.setText("Numero de Cédula :");

        textoNumCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textoNumCedulaActionPerformed(evt);
            }
        });
        textoNumCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textoNumCedulaKeyTyped(evt);
            }
        });

        jLabel3.setText("Nombre :");

        jLabel4.setText("Apellido :");

        jLabel5.setText("Sexo :");

        sexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Femenino" }));

        jLabel6.setText("Dirección :");

        textoDireccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textoDireccionActionPerformed(evt);
            }
        });

        jLabel7.setText("Teléfono :");

        jLabel8.setText("Especialidad :");

        agregarEspecialidad.setText("+");
        agregarEspecialidad.setToolTipText("");
        agregarEspecialidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarEspecialidadActionPerformed(evt);
            }
        });

        jLabel9.setText("Correo :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textoNumRegistro)
                    .addComponent(textoNumCedula)
                    .addComponent(textoNombre)
                    .addComponent(textoApellido)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(sexo, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(textoDireccion)
                    .addComponent(textoTelefono)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(especialidad, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addComponent(agregarEspecialidad))
                    .addComponent(textoCorreo))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textoNumRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textoNumCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(textoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(textoApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(sexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(textoDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(textoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(especialidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(agregarEspecialidad)))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(textoCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        Confirmar.setText("Confirmar");
        Confirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConfirmarActionPerformed(evt);
            }
        });

        Cancelar.setText("Cancelar");
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(116, 116, 116)
                        .addComponent(Confirmar)
                        .addGap(90, 90, 90)
                        .addComponent(Cancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Confirmar)
                    .addComponent(Cancelar))
                .addGap(41, 41, 41))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void textoNumCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textoNumCedulaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textoNumCedulaActionPerformed

    private void textoDireccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textoDireccionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textoDireccionActionPerformed

    private void agregarEspecialidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarEspecialidadActionPerformed
        AgregarEspecialidad agrEsp = new AgregarEspecialidad(this, true);
        agrEsp.setLocationRelativeTo(this);
        agrEsp.setVisible(true);
        actualizarEspercialidades();
    }//GEN-LAST:event_agregarEspecialidadActionPerformed

    private void textoNumCedulaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textoNumCedulaKeyTyped
       validarEnteros(evt);
    }//GEN-LAST:event_textoNumCedulaKeyTyped

    private void ConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConfirmarActionPerformed
       
        int numEsp = especialidad.getSelectedIndex(); 
        List listEspecialidades = dao.ejecutarQuery("from Especialidad where id_especialidad = " + Integer.toString(++numEsp));
        Especialidad esp = (Especialidad) listEspecialidades.get(0);
        
        Medico medicoActual = new Medico();
        medicoActual.setNumRegistro(Integer.parseInt(textoNumRegistro.getText()));
        medicoActual.setCedula(Integer.parseInt(textoNumCedula.getText()));
        medicoActual.setNombre(textoNombre.getText());
        medicoActual.setApellido(textoApellido.getText());
        medicoActual.setDireccion(textoDireccion.getText());
        medicoActual.setEspecialidad(esp);
        medicoActual.setSexo(sexo.getItemAt(sexo.getSelectedIndex()).toString());
        medicoActual.setTelefono(textoTelefono.getText());
        medicoActual.setCorreo(textoCorreo.getText());
        
        if(medico.retornarElementosString().equals(medicoActual.retornarElementosString())) 
            JOptionPane.showMessageDialog(this, "No se realizaron Cambios");
                  
        else {
            int resp = JOptionPane.showConfirmDialog(this, "Esta seguro que desea realizar los cambios?");
            if(resp == 0) dao.modificar(medicoActual);
        }
    
    }//GEN-LAST:event_ConfirmarActionPerformed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        
    }//GEN-LAST:event_formKeyPressed

    private void textoNumRegistroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textoNumRegistroKeyPressed

    }//GEN-LAST:event_textoNumRegistroKeyPressed

    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_CancelarActionPerformed

    private void textoNumRegistroKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textoNumRegistroKeyTyped
        validarEnteros(evt);
    }//GEN-LAST:event_textoNumRegistroKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ModificarMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ModificarMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ModificarMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ModificarMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ModificarMedico dialog = new ModificarMedico(new javax.swing.JDialog(), true, medico);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
   
    private void actualizarEspercialidades(){
        especialidad.removeAllItems();
        List Esp = dao.ejecutarQuery("select descripcion from Especialidad");
        for(Object especialidades : Esp){
            especialidad.addItem(especialidades);
        }
    }
    
    public void validarEnteros(java.awt.event.KeyEvent evt){
        if(!Character.isDigit(evt.getKeyChar()) && !Character.isISOControl(evt.getKeyChar()))
            {
                 Toolkit.getDefaultToolkit().beep();
                 evt.consume();
             }     
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancelar;
    private javax.swing.JButton Confirmar;
    private javax.swing.JButton agregarEspecialidad;
    private javax.swing.JComboBox especialidad;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox sexo;
    private javax.swing.JTextField textoApellido;
    private javax.swing.JTextField textoCorreo;
    private javax.swing.JTextField textoDireccion;
    private javax.swing.JTextField textoNombre;
    private javax.swing.JTextField textoNumCedula;
    private javax.swing.JTextField textoNumRegistro;
    private javax.swing.JTextField textoTelefono;
    // End of variables declaration//GEN-END:variables
}
